#include "singlelist.h"
#include <iostream>
using namespace singlelist;

ListElmt::ListElmt(int data)
{
    this->m_data = data;
    this->m_next = NULL;
}

void ListElmt::SetData(int d)
{
    this->m_data = d;
}

void ListElmt::SetNext(ListElmt* e)
{
    this->m_next = e;
}

int ListElmt::GetData()
{
    return this->m_data;
}

ListElmt* ListElmt::GetNext()
{
    return this->m_next;
}

SingleList::SingleList()
{
    this->m_head = NULL;
    this->m_tail = NULL;
    this->m_size = 0;
}

SingleList::~SingleList()
{
    if (this->m_head != NULL)
    {
         ListElmt* cur = NULL;
         while (this->m_head)
         {
             cur = this->m_head;
             this->m_head = this->m_head->GetNext();
             delete cur;
         }
    }
}

ListElmt* SingleList::ListHead()
{
    return this->m_head;
}

ListElmt* SingleList::ListTail()
{
    return this->m_tail;
}

void SingleList::ListInsert(ListElmt* e)
{
    if (e == NULL)
        return;

    ++this->m_size;
    if (this->m_head == NULL)
    {
        this->m_head = e;
        this->m_tail = e;

        return;
    }

    this->m_tail->SetNext(e);
    this->m_tail = e;
}

ListElmt* SingleList::ListDelNext(ListElmt* e)
{
   ListElmt* del = NULL;
   if (this->m_size == 0)
   {
       return del;
   }

   if (e == NULL)//delete head
   {
        del = this->m_head;
        this->m_head = this->m_head->GetNext();
        if (this->m_size == 1)
        {
            this->m_tail = NULL;
        }
   }
   else
    {
        del = e->GetNext();
        if (del != NULL)
        {
            e->SetNext(del->GetNext());
        }
    }

    --this->m_size;
    return del;
}

int SingleList::GetListSize()
{
    return this->m_size;
}

void singlelist::PrintSingleList(SingleList* l)
{
    std::cout << "cur data: ";
    ListElmt* head = l->ListHead();
    while(head)
    {
        std::cout << head->GetData() << " ";
        head = head->GetNext();
    }

    std::cout << std::endl;
    std::cout << "singlelist size: " << l->GetListSize() << std::endl;
}
